<template>
  <div>
    <el-card class="box-card">
      <!-- 添加员工按钮 -->
      <div slot="header" class="clearfix">
        <el-button style="float: right" size="small" type="primary" @click="addFn">添加员工</el-button>
      </div>

      <!-- 表单 -->
      <el-table border style="width: 100%" :data="list">
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="邮箱" prop="email" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button type="primary" size="small" @click="setRole(row.id)">设置角色</el-button>
            <el-button type="success" size="small" @click="editorFn(row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="limit"
          @current-change="curChangeFn"
        />
      </el-row>
    </el-card>

    <!-- 新增和编辑员工 -->
    <AddEditor ref="employeesForm" :show-dialog.sync="showDialog" @updateEvent="getUserList" />

    <!-- 设置角色 -->
    <SetRole ref="assignRole" :show-role-dialog.sync="showRoleDialog" />
  </div>
</template>

<script>
import { getEmployeeList, getEmployeeNum, getSpecifEmployee, delEmployee } from '@/api/employees'
import AddEditor from './components/add-editor.vue'
import SetRole from './components/set-role.vue'

export default {
  components: { AddEditor, SetRole },
  data() {
    return {
      list: [],
      total: 0, // 用户数量
      limit: 10,
      start: 0,
      showDialog: false, // 新增和编辑弹窗
      showRoleDialog: false // 设置角色弹窗
    }
  },
  created() {
    this.getUserList()
  },
  methods: {

    // -----封装获取用户列表函数-----
    async getUserList() {
      const res = await getEmployeeList({
        _limit: this.limit, // 每页的获取条数
        _start: this.start // 开始获取的位置
      })
      this.list = res.data
      // 获取用户数量
      const reslut = await getEmployeeNum()
      this.total = reslut.data
    },

    // -----页面发生变化时-----
    curChangeFn(page) {
      this.start = page === 1 ? 0 : (page - 1) * this.limit
      this.getUserList()
    },

    // -----新增员工按钮-----
    addFn() {
      this.showDialog = true
    },

    // -----编辑按钮-----
    async editorFn(id) {
      this.showDialog = true
      const res = await getSpecifEmployee(id)
      // 数据回填
      this.$refs.employeesForm.formData = res.data
    },

    // -----删除按钮-----
    delFn(id) {
      this.$confirm('请问确认删除该员工吗?', '提示', {
        type: 'warning'
      }).then(async() => {
        // 调用接口并刷新列表
        await delEmployee(id)
        this.getUserList()
        // 提示
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // -----设置角色按钮-----
    setRole(id) {
      this.showRoleDialog = true
      this.$nextTick(() => {
        // 把id传到子组件
        this.$refs.assignRole.getRoleIds(id)
      })
    }
  }
}
</script>

<style scoped>
  .box-card {
    margin: 20px;
  }

  .el-card__header{
    padding: 15px;
    border-bottom:2px solid #EBEEF5
  }

  .el-button{
    margin-left: 15px;
  }
</style>
